package owg.util.data;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.util.ArrayList;

/**Exposes methods to handle PFields. The implementation should declare all PFields as final and initialize them in the class body or constructor.
 * <br/>
 * <br/>
 * PFields that should not be exposed must be annotated with the {@link Ignore} annotation.*/
public interface PStructure
{
	/**Annotation for ChildField fields which should not be included in {@link LapNode#getChildFields()}*/
	@Retention(RetentionPolicy.RUNTIME) 
	public @interface Ignore {}
	/**Annotation for ChildField fields which should be on a new line in the UI*/
	
	
	public default PField<?>[] getFields()
	{
		try
		{
			ArrayList<PField<?>> list = new ArrayList<>();
			for(Field f : getClass().getDeclaredFields())
			{
				//Ignore fields marked with Ignore
				if(f.getAnnotation(Ignore.class) != null)
					continue;
				
				if(PField.class.isAssignableFrom(f.getType()))
				{
					f.setAccessible(true);
					PField<?> cf = PField.class.cast(f.get(this));
					list.add(cf);
				}
			}
			return list.toArray(new PField[list.size()]);
		}
		catch (IllegalArgumentException | IllegalAccessException e)
		{
			throw new RuntimeException(e);
		}
	}
}
